package asynchronous;


import java.util.concurrent.ArrayBlockingQueue;

//订阅发布
public class PBModel {
    static ArrayBlockingQueue queue = new ArrayBlockingQueue<String>(10);
    public static void publish(String message){
        try {
            queue.put(message);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void subscribe(){
        try {
            System.out.println(queue.take()); ;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread server = new Thread(()->{
            while (true){
                subscribe();
            }
        });
        server.start();
        publish("hello");
        Thread.sleep(1000);
        publish("world");
        while (true){
            Thread.sleep((int)(Math.random() *  5000));
            publish("message");
        }

    }
}
